package com.movie.controller;

import com.movie.dto.ApiResponse;
import com.movie.dto.PageResponse;

import com.movie.dto.SystemUserDTO;
import com.movie.service.SystemUserService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/system/users")
@RequiredArgsConstructor
@Tag(name = "用户管理", description = "系统用户的增删改查接口")
public class SystemUserController {
    
    private final SystemUserService userService;
    
    @GetMapping
    @Operation(summary = "获取用户列表", description = "分页获取用户列表，支持搜索")
    public ApiResponse<PageResponse<SystemUserDTO>> getUserList(
            @Parameter(description = "页码，从0开始") @RequestParam(defaultValue = "0") int page,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") int size,
            @Parameter(description = "搜索关键词") @RequestParam(required = false) String keyword) {
        Pageable pageable = PageRequest.of(page, size);
        Page<SystemUserDTO> userPage = userService.findByKeyword(keyword, pageable);
        return ApiResponse.success(PageResponse.of(userPage));
    }
    
    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取用户", description = "根据用户ID获取具体的用户信息")
    public ApiResponse<SystemUserDTO> getUserById(
            @Parameter(description = "用户ID") @PathVariable Long id) {
        SystemUserDTO user = userService.findById(id);
        return ApiResponse.success(user);
    }
    
    @PostMapping
    @Operation(summary = "创建用户", description = "创建新的系统用户")
    public ApiResponse<SystemUserDTO> createUser(
            @Parameter(description = "用户信息") @RequestBody SystemUserDTO SystemUserDTO) {
        SystemUserDTO created = userService.create(SystemUserDTO);
        return ApiResponse.success("用户创建成功", created);
    }
    
    @PutMapping("/{id}")
    @Operation(summary = "更新用户", description = "根据ID更新用户信息")
    public ApiResponse<SystemUserDTO> updateUser(
            @Parameter(description = "用户ID") @PathVariable Long id,
            @Parameter(description = "用户信息") @RequestBody SystemUserDTO SystemUserDTO) {
        SystemUserDTO updated = userService.update(id, SystemUserDTO);
        return ApiResponse.success("用户更新成功", updated);
    }
    
    @DeleteMapping("/{id}")
    @Operation(summary = "删除用户", description = "根据ID删除用户")
    public ApiResponse<Void> deleteUser(
            @Parameter(description = "用户ID") @PathVariable Long id) {
        userService.delete(id);
        return ApiResponse.success("用户删除成功", null);
    }
    
    @PutMapping("/{id}/status")
    @Operation(summary = "切换用户状态", description = "切换用户的启用/禁用状态")
    public ApiResponse<SystemUserDTO> toggleUserStatus(
            @Parameter(description = "用户ID") @PathVariable Long id) {
        SystemUserDTO updated = userService.toggleStatus(id);
        return ApiResponse.success("用户状态切换成功", updated);
    }
}